<?php

class Menu extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('admin/m_menu');
        $this->load->library('pagination');
		$this->load->helper('tgl_indonesia');
		

	}
	
	function index(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_menu->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/pengurus/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$data['tabel'] = $this->m_menu->getTable($config['per_page'], $this->uri->segment(4));
			$data['main_view'] = 'admin/managemen_menu/index';
			$this->load->view('admin/template', $data);
		}
	}
	
	function add(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$data['main_view'] = 'admin/managemen_menu/add';
			$this->load->view('admin/template', $data);
		}
	}
	
	function insert(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
			
			$this->form_validation->set_rules('menu', 'Nama Pengurus', 'required');
			$this->form_validation->set_rules('url', 'Nama Pengurus', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			if($this->form_validation->run()==true){
				$menu = $this->input->post('menu');
				$url = $this->input->post('url');
				
				$this->m_menu->create($menu,$url);
				redirect('admin/menu');
				
			}else{
				$data['main_view'] = 'admin/managemen_menu/add';
				$this->load->view('admin/template', $data);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function edit(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			//$id = $this->uri->segment();
			$data['data'] = $this->m_menu->getDataById($this->uri->segment(4));
			$data['main_view'] = 'admin/managemen_menu/edit';
			$this->load->view('admin/template', $data);
		}
	}
	
	function update($id){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('menu', 'Menu', 'required');
			$this->form_validation->set_rules('url', 'URL', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			if($this->form_validation->run()==true){
				$id = $this->input->post('id');
				$menu = $this->input->post('menu');
				$url = $this->input->post('url');
				$status = $this->input->post('status');
				//$status = $this->input->post('status');
				
				$this->m_menu->update($id,$menu,$url, $status);
				redirect('admin/menu');
			}else{
				redirect('admin/menu/edit/' . $id);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function delete(){
		$id = $this->uri->segment(4);
		$this->m_menu->delete($id);
		redirect('admin/menu');
	}
	
	function enable(){
		$id = $this->uri->segment(4);
		$this->m_menu->setEnable($id, 'enable');
		redirect('admin/menu');
	}
	
	function disable(){
		$id = $this->uri->segment(4);
		$status = "disable";
		$this->m_menu->setDisable($id, $status);
		redirect('admin/menu');
	}
	
	function search(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_menu->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/managemen_menu/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$judul = $this->input->post('judul');
			
			$data['tabel'] = $this->m_menu->search($judul);
			$data['main_view'] = 'admin/managemen_menu/index';
			$this->load->view('admin/template', $data);
		}	
	}

}